import { useMutation, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
import { deleteCabin as deleteCabinApi } from '../../services/apiCabins.js'

export function useDeleteCabin() {
	const queryClient = useQueryClient()

	const { isPending: isDeleting, mutate: deleteCabin } = useMutation({
		mutationFn: (id) => deleteCabinApi(id),
		onSuccess: () => {
			toast.success('Delete cabin successfully!')
			queryClient.invalidateQueries({
				queryKey: ['cabins']
			})
		},
		onError: (err) => {
			toast.error('Delete cabin failed', err.message)
		}
	})

	return {
		isDeleting,
		deleteCabin
	}
}
